package eyrolles;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modele.Employe;
import modele.EmployeDB;
import modele.InterfaceEmploye;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

public class EditEmployeAction extends ActionDB {

    protected void updateUser(ActionForm form)
            throws SQLException {

        String user = null;
        Employe employeUpdate = null;

        try {

           EmployeForm eForm = (EmployeForm) form;
           conn = getConnection();
            
           employeUpdate = new Employe();
           employeUpdate.setUsername(eForm.getUsername());
           employeUpdate.setPassword(eForm.getPassword());
           employeUpdate.setRoleid(Integer.parseInt(eForm.getRoleid()));
           employeUpdate.setDepid(Integer.parseInt(eForm.getDepid()));
           employeUpdate.setName(eForm.getName());
           employeUpdate.setPhone(eForm.getPhone());
           employeUpdate.setEmail(eForm.getEmail());

           InterfaceEmploye employeDB = new EmployeDB();
           employeDB.connect(conn);
           employeDB.updateEmploye(employeUpdate);       
            
          
        }catch (NullPointerException e) {
            
            throw new SQLException();
        }
        finally {

            freeConnection(conn);
        }
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        // Cible par defaut en cas de succes
        String target = new String("success");

        ActionMessages errors = new ActionMessages();


        // Teste sur l'identification de l'utilisateur
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.required"));


            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {

            // Action annulee
            return (mapping.findForward(target));
        }

        try {

            updateUser(form);
        } catch (SQLException e) {

            target = new String("error");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));

        }

        // Signaler les erreurs eventuelles
        if (!errors.isEmpty()) {

            saveErrors(request, errors);
        }
        
        // Transmettre la requete a la vue appropriee
        return (mapping.findForward(target));
    }
}